package foliotracker.tests;

import static org.junit.Assert.*;

import java.util.Date;

import org.junit.Before;
import org.junit.Test;

import foliotracker.Portfolio;
import foliotracker.Stock;

public class PortfolioTest
{
	private Portfolio portfolio;
	
	@Before
	public void setup()
	{
		portfolio = new Portfolio("Name");
	}
	
	@Test
	public void getNameTest()
	{
		assertEquals("Name", portfolio.getName());
		
		portfolio.setName("Name2");
		assertEquals("Name2", portfolio.getName());
	}
	
	@Test
	public void buyStockTest()
	{
		portfolio.buyStock("MSFT", "Microsoft", new Date(), 10, 11, 12);
		
		//after buying a stock, number of stocks should be 1
		assertEquals(1, portfolio.getStocks().size());
		
		//make sure the values were copied correctly
		Stock stock = portfolio.getStocks().get(0);
		assertEquals("MSFT", stock.getSymbol());
		assertEquals("Microsoft", stock.getName());
		assertEquals(10, stock.getQuantity());
		assertEquals(110, stock.getInitialCost(), 0.01);
		assertEquals(12, stock.getPrice(), 0.01);
		
		//buy more of the same, make sure there's still only one stock
		portfolio.buyStock("MSFT", "Microsoft", new Date(), 10, 11, 14);
		assertEquals(1, portfolio.getStocks().size());
		
		//make sure the values were updated correctly
		assertEquals("MSFT", stock.getSymbol());
		assertEquals("Microsoft", stock.getName());
		assertEquals(20, stock.getQuantity());
		assertEquals(220, stock.getInitialCost(), 0.01);
		assertEquals(14, stock.getPrice(), 0.01);
		
		//buy a different stock, make sure it has been added
		portfolio.buyStock("GOOG", "Google", new Date(), 1, 2, 3);
		assertEquals(2, portfolio.getStocks().size());
		
		//make sure it didn't change the first stock
		assertEquals("MSFT", stock.getSymbol());
		assertEquals("Microsoft", stock.getName());
		assertEquals(20, stock.getQuantity());
		assertEquals(220, stock.getInitialCost(), 0.01);
		assertEquals(14, stock.getPrice(), 0.01);
		
		//make sure the values were copied correctly
		stock = portfolio.getStocks().get(1);
		assertEquals("GOOG", stock.getSymbol());
		assertEquals("Google", stock.getName());
		assertEquals(1, stock.getQuantity());
		assertEquals(2, stock.getInitialCost(), 0.01);
		assertEquals(3, stock.getPrice(), 0.01);
	}

	@Test
	public void sellStockTest()
	{
		portfolio.buyStock("MSFT", "Microsoft", new Date(), 10, 11, 12);
		portfolio.buyStock("GOOG", "Google", new Date(), 1, 2, 3);
		
		//sell some MS stock and check the quantity is correct
		portfolio.sellStock("MSFT", 3);
		assertEquals(2, portfolio.getStocks().size());
		assertEquals(7, portfolio.getStocks().get(0).getQuantity());
		
		//make sure it didn't touch the google stock
		assertEquals(1, portfolio.getStocks().get(1).getQuantity());
		
		//sell the rest and make sure the symbol is removed
		portfolio.sellStock("MSFT", 7);
		assertEquals(1, portfolio.getStocks().size());
	}
	
	@Test(expected = IllegalArgumentException.class)
	public void sellStockInvalidSymbolTest()
	{
		portfolio.sellStock("XXXX", 10);
	}
	
	@Test(expected = IllegalArgumentException.class)
	public void sellStockInvalidQuantityTest()
	{
		portfolio.buyStock("MSFT", "Microsoft", new Date(), 4, 5, 6);
		portfolio.sellStock("MSFT", 10);
	}
	
	@Test
	public void getTotalValueTest()
	{
		portfolio.buyStock("MSFT", "Microsoft", new Date(), 10, 11, 12);
		portfolio.buyStock("GOOG", "Google", new Date(), 1, 2, 3);
		
		assertEquals(123, portfolio.getTotalValue(), 0.01);
	}
	
	@Test
	public void getTotalInitialCostTest()
	{
		portfolio.buyStock("MSFT", "Microsoft", new Date(), 10, 11, 12);
		portfolio.buyStock("GOOG", "Google", new Date(), 1, 2, 3);
		
		assertEquals(112, portfolio.getTotalInitialCost(), 0.01);
	}
	
	@Test
	public void getNetProfitTest()
	{
		portfolio.buyStock("MSFT", "Microsoft", new Date(), 10, 11, 12);
		portfolio.buyStock("GOOG", "Google", new Date(), 1, 2, 3);
		
		assertEquals(11, portfolio.getNetProfit(), 0.01);
	}
	
	
	@Test
	public void getStockBySymbolTest(){
		portfolio.buyStock("MSFT", "Microsoft", new Date(), 10, 11, 12);
		portfolio.buyStock("GOOG", "Google", new Date(), 1, 2, 3);
		
		Stock stock = portfolio.getStockBySymbol("GOOG");
		assertNotNull(stock);
		assertEquals("GOOG", stock.getSymbol());
		assertEquals("Google", stock.getName());
		assertEquals(1, stock.getQuantity());
		assertEquals(2, stock.getInitialCost(), 0.01);
		assertEquals(3, stock.getPrice(), 0.01);
		
		stock = portfolio.getStockBySymbol("XXXX");
		assertNull(stock);
		
	}
	
	@Test
	public void getPercentageGainTest()
	{
		portfolio.buyStock("MSFT", "Microsoft", new Date(), 5, 10, 15);
		portfolio.buyStock("GOOG", "Google", new Date(), 10, 20, 30);
		
		assertEquals(0.5, portfolio.getPercentageGain(), 0.01);		
		
	}
}
